package day_7_17;

import java.util.ArrayList;
import java.util.List;

public class Main2 {
    /**
     * 228. 汇总区间
     * https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150
     */

    public List<String> summaryRanges(int[] nums) {
        List<String> ret = new ArrayList<>();
        int i = 0;
        int n = nums.length;
        while(i < n) {
            int low = i;
            i++;
            while(i < n && nums[i] == nums[i - 1] + 1) {
                i++;
            }
            // 这时候，i-1位置就是当前区间的最后一位
            int end = i - 1;
            StringBuffer str = new StringBuffer(nums[low] + "");
            if(low < end) {
                str.append("->");
                str.append(nums[end] + "");
            }
            ret.add(str.toString());
        }
        return ret;
    }
}
